<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- <page-tools :has-before="false"> -->
      <page-tools>
        <template #after>
          <el-button type="primary" size="small" @click="onAdd({pid:'0',type:1})">新增权限</el-button>
        </template>
      </page-tools>
      <!-- el-table[:data :border="true"]>el-table-column[label prop=""]*4 -->
      <el-table :data="list" :border="true" row-key="id">
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="标识" prop="code"></el-table-column>
        <el-table-column label="描述" prop="description"></el-table-column>
        <el-table-column label="操作" prop="">
          <template v-slot="{row}">
            <el-button type="text" @click="onAdd({pid:row.id,type:2})">添加</el-button>
            <el-button type="text" @click="onEdit(row.id)">编辑</el-button>
            <el-button type="text" @click="onDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <addperm v-if="showDialog" ref="addPermRef" :visible="showDialog" @close="showDialog=false" @success="getPermissionList"></addperm>
  </div>
</template>

<script>
import { getPermissionList, getPermissionDetail } from '@/api/permission.js'
import { translateListToTree } from '@/utils/index.js'
import addperm from './components/add-perm.vue'
import { delDepartments } from '../../api/departments'
export default {
  name: 'PermissionPage',
  components: {
    addperm
  },
  data() {
    return {
      list: [], // 列表数据
      showDialog: false // 控制对话框显示
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 编辑
    async onEdit(id) {
      // 打开对话框
      this.showDialog = true
      // this.title = '编辑权限'
      // 回显数据
      this.$refs.addPermRef.formDate = await getPermissionDetail(id)
    },
    // 删除
    onDel(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        await delDepartments(id)
        this.$message.success('操作成功')
        // 刷新列表
        this.getPermissionList()
      }).catch(() => {
      })
    },
    // 打开对话框
    async onAdd(data) {
      this.showDialog = true
      // this.title = '新增权限'
      // 回调写法
      // this.$nextTick(() => {
      //   this.$refs.addPermRef.form.pid = data.pid
      //   this.$refs.addPermRef.form.type = data.type
      // })
      // 异步写法
      await this.$nextTick()
      // this.$refs.addPermRef.form.pid = data.pid
      //   this.$refs.addPermRef.form.type = data.type
      Object.assign(this.$refs.addPermRef.formDate, data)
    },
    // 获取权限
    async getPermissionList() {
      this.list = translateListToTree(await getPermissionList(), '0')
      console.log(this.list)
    }

  }
}
</script>

<style>

</style>
